package com.szosen.jkzj.activity;

import java.io.File;
import java.util.LinkedList;
import java.util.Random;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.szosen.jkzj.R;
import com.szosen.util.SharedpreferenceUtil;
import com.szosen.util.ToastTool;

public class YaowulistsActivity extends BaseActivity {
	private String from = "";
	private LinkedList<String> datas;
	private MyAdapter mAdapter;
	private EditText et_yaowu;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_yaowulists);
		initData();
		super.setTitleContent("【药物列表】");
	}

	private void initData() {
		from = getIntent().getStringExtra("from");
		datas = new LinkedList<String>();
		et_yaowu = (EditText) findViewById(R.id.yaowulists_et_yaowu);
		// 判断from来自高血压还是糖尿病。然后获取sharepreference数据。填充
		String[] yaowudatas = null;
		switch (from) {
		case SharedpreferenceUtil.YAOWUDATAS_GXY:
			String yaowudatas_gxy = SharedpreferenceUtil.getYaowuDatas(context,
					"", SharedpreferenceUtil.YAOWUDATAS_GXY);
			yaowudatas = yaowudatas_gxy.split(SharedpreferenceUtil.YAOWUDATAS_SPLIT);
			for (String data : yaowudatas) {
				datas.add(data);
			}
			break;
		case SharedpreferenceUtil.YAOWUDATAS_TNB:
			String yaowudatas_tnb = SharedpreferenceUtil.getYaowuDatas(context,
					"", SharedpreferenceUtil.YAOWUDATAS_TNB);
			yaowudatas = yaowudatas_tnb.split(SharedpreferenceUtil.YAOWUDATAS_SPLIT);
			for (String data : yaowudatas) {
				datas.add(data);
			}
			break;
		default:
			break;
		}
		ListView lv_show = (ListView) findViewById(R.id.yaowulists_lv);
		mAdapter = new MyAdapter(datas);
		lv_show.setAdapter(mAdapter);
		lv_show.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					final int position, long id) {
				AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
				dialogBuilder.setPositiveButton("确定",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								mAdapter.removeData(position);
								mAdapter.notifyDataSetChanged();
							}
						});
				dialogBuilder.setNegativeButton("取消",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								dialog.cancel();
							}
						});
				dialogBuilder.setCancelable(false);
				dialogBuilder.setTitle("提示").setMessage("你确定要删除这条药物信息吗?");
				AlertDialog dialog = dialogBuilder.create();
				dialog.show();
				return false;
			}
		});
	}

	public void addYaowu(View view) {
		String yaowu = et_yaowu.getText().toString();
		if (!TextUtils.isEmpty(yaowu)) {
			mAdapter.addData(yaowu);
			et_yaowu.setText("");
		} else {
			ToastTool.showShort(context, "请先输入药物名称");
		}
	}

	public void saveAndEdit(View view) {
		// 保存列表。
		saveYaoDatas();
		finish();
	}
	@Override
	public void onBackPressed() {
		saveYaoDatas();
		super.onBackPressed();
	}
	private void saveYaoDatas() {
		LinkedList<String> results = mAdapter.getDatas();
		switch (from) {
		case SharedpreferenceUtil.YAOWUDATAS_GXY:
			StringBuilder sbgxy = new StringBuilder();
			for (int i = 0; i < results.size(); i++) {
				String s = results.get(i);
				sbgxy.append(s);
				sbgxy.append(SharedpreferenceUtil.YAOWUDATAS_SPLIT);
			}
			String resultgxy = sbgxy.toString();
			if (resultgxy.endsWith(SharedpreferenceUtil.YAOWUDATAS_SPLIT)) {
				resultgxy = resultgxy.substring(0, resultgxy.length() - 1);
			}
			SharedpreferenceUtil.setYaowuDatas(context, resultgxy,
					SharedpreferenceUtil.YAOWUDATAS_GXY);
			break;
		case SharedpreferenceUtil.YAOWUDATAS_TNB:
			StringBuilder sbtnb = new StringBuilder();
			for (int i = 0; i < results.size(); i++) {
				String s = results.get(i);
				sbtnb.append(s);
				sbtnb.append(SharedpreferenceUtil.YAOWUDATAS_SPLIT);
			}
			String resulttnb = sbtnb.toString();
			if (resulttnb.endsWith(SharedpreferenceUtil.YAOWUDATAS_SPLIT)) {
				resulttnb = resulttnb.substring(0, resulttnb.length() - 1);
			}
			SharedpreferenceUtil.setYaowuDatas(context, resulttnb,
					SharedpreferenceUtil.YAOWUDATAS_TNB);
			break;

		default:
			break;
		}
	}

	private class MyAdapter extends BaseAdapter {
		private LinkedList<String> lists = new LinkedList<String>();
		private Holder holder;

		public MyAdapter(LinkedList<String> datas) {
			this.lists = datas;
		}

		public void removeData(int position) {
			lists.remove(position);
			this.notifyDataSetChanged();
		}
		public LinkedList<String> getDatas(){
			return lists;
		}
		public void addData(String data) {
			lists.addFirst(data);
			this.notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			return lists.size();
		}

		@Override
		public Object getItem(int position) {
			return lists.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				holder = new Holder();
				convertView = (LinearLayout) LayoutInflater.from(
						parent.getContext()).inflate(
						R.layout.listview_item_yaowu, null);
				TextView textView = (TextView) convertView
						.findViewById(R.id.listview_item_tv);
				holder.textView = textView;
				convertView.setTag(holder);
			} else {
				holder = (Holder) convertView.getTag();
			}
			holder.textView.setText((String) getItem(position));
			return convertView;
		}
	}

	public class Holder {
		public TextView textView;
	}
}
